package com.zhou.jianzhi.service.impl;



import com.aliyun.oss.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.jianzhi.entity.base.PageResult;
import com.zhou.jianzhi.entity.po.ForumPost;
import com.zhou.jianzhi.entity.po.ForumReply;
import com.zhou.jianzhi.mapper.ForumPostMapper;
import com.zhou.jianzhi.mapper.ForumReplyMapper;
import com.zhou.jianzhi.service.ForumPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 黄嘉杰
* @description 针对表【forum_post(论坛帖子表)】的数据库操作Service实现
* @createDate 2025-01-23 11:15:03
*/
@Service
public class ForumPostServiceImpl extends ServiceImpl<ForumPostMapper, ForumPost>
implements ForumPostService {

    @Autowired
    private ForumPostMapper forumPostMapper;

    @Autowired
    private ForumReplyMapper forumReplyMapper;

    @Override
    public PageResult<ForumPost> getPostList(String type, Integer page, Integer size) {
        try {
            // 构建查询条件
            LambdaQueryWrapper<ForumPost> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ForumPost::getStatus, 1);  // 只查询正常状态的帖子

            // 如果指定了类型且不是"全部"，则添加类型筛选
            if (StringUtils.isNotBlank(type) && !"all".equals(type)) {
                queryWrapper.eq(ForumPost::getType, type);
            }

            // 按创建时间倒序排序
            queryWrapper.orderByDesc(ForumPost::getCreateTime);

            // 分页查询
            Page<ForumPost> pageParam = new Page<>(page, size);
            Page<ForumPost> postPage = forumPostMapper.selectPage(pageParam, queryWrapper);

            return new PageResult<>(
                    postPage.getRecords(),
                    postPage.getTotal(),
                    postPage.getCurrent(),
                    postPage.getPages()
            );
        } catch (Exception e) {
            log.error("获取帖子列表失败", e);
            throw new ServiceException("获取帖子列表失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPost(ForumPost post) {
        try {
            // 设置初始值
            post.setCreateTime(new Date());
            post.setUpdateTime(new Date());
            post.setReplyCount(0);
            post.setStatus(1);

            // 插入帖子
            forumPostMapper.insert(post);
        } catch (Exception e) {
            log.error("发布帖子失败", e);
            throw new ServiceException("发布帖子失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editPost(ForumPost post) {
        try {
            // 检查帖子是否存在
            ForumPost existPost = forumPostMapper.selectById(post.getId());
            if (existPost == null || existPost.getStatus() == 0) {
                throw new ServiceException("帖子不存在");
            }

            // 检查是否是帖子作者
            if (!existPost.getAuthor().equals(post.getAuthor())) {
                throw new ServiceException("无权修改他人的帖子");
            }

            // 更新帖子
            post.setUpdateTime(new Date());
            forumPostMapper.updateById(post);
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("修改帖子失败", e);
            throw new ServiceException("修改帖子失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletePost(Long id) {
        try {
            // 软删除帖子
            ForumPost post = new ForumPost();
            post.setId(id);
            post.setStatus(0);
            forumPostMapper.updateById(post);

            // 软删除相关回复
            ForumReply reply = new ForumReply();
            reply.setStatus(0);
            LambdaQueryWrapper<ForumReply> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ForumReply::getPostId, id);
            forumReplyMapper.update(reply, queryWrapper);
        } catch (Exception e) {
            log.error("删除帖子失败", e);
            throw new ServiceException("删除帖子失败");
        }
    }

    @Override
    public ForumPost getPostDetail(Long id) {
        try {
            // 查询帖子详情
            ForumPost post = forumPostMapper.selectById(id);
            if (post == null || post.getStatus() == 0) {
                throw new ServiceException("帖子不存在");
            }
            return post;
        } catch (ServiceException e) {
            throw e;
        } catch (Exception e) {
            log.error("获取帖子详情失败", e);
            throw new ServiceException("获取帖子详情失败");
        }
    }
}
